package com.lunesu.bjair;

import java.io.IOException;

import com.lunesu.bjair.R;

import android.content.Context;
import android.os.Handler;

public class UpdateThread extends Thread {
	final Handler handler = new Handler();
	final String baseUrl;
	final StringBuilder sb = new StringBuilder();
	final ICallback callback;
	final Context context;
	
	public UpdateThread(Context context, ICallback callback) {
		this.context = context;
		this.baseUrl = context.getString(R.string.bjair_url);
		this.callback = callback;
	}

	@Override
	public void run() {
		int airquality = -1;
		try {
			StringBuilder sb = null;
			if (callback != null)
				sb = new StringBuilder();
			// Get the air quality from HTTP and record the entire page into a string
			airquality = BeijingAirAppWidgetProvider.getAirQuality(baseUrl, sb);
		} catch (IOException e) {
			//Log.e("error", "BeijingAirActivity.onStart", e);
		}
		// Execute the 'delegate' in the UI thread, so it can update the webview
		if (callback != null) {
			callback.
			handler.post(callback);
		}
		// Update the app-widget (apparently this works fine from this thread)
		BeijingAirAppWidgetProvider.updateWidget(context, airquality);
	}

}
